﻿// Extensions Made Easy - Copyright 2012 by Jan Van der Haegen - All rights reserved, use for educational purposes only.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using ExtensionsMadeEasy.ClientAPI.Utilities.Base;
using ExtensionsMadeEasy.ClientAPI.Utilities.Extensions;
using Microsoft.LightSwitch.BaseServices.Notifications;
using Microsoft.LightSwitch.Runtime.Shell;

namespace ExtensionsMadeEasy.Navigation
{
    [Export(typeof(IFavoriteOpenedScreenListener))]
    public class FavoriteOpenedScreenListener : NotifyPropertyChangedBase, IFavoriteOpenedScreenListener
    {
        private class StringComparer : IComparer<string>
        {
            public List<string> recentlyOpenedScreens { get; set; }
            public int Compare(string x, string y)
            {
                int xCount = this.recentlyOpenedScreens.Count((s) => (s.Equals(x)));
                int YCount = this.recentlyOpenedScreens.Count((s) => (s.Equals(y)));
                return xCount == YCount ? 0 : (xCount > YCount ? 1 : -1);
            }
        }

        private readonly string key;
        private readonly List<string> recentlyOpenedScreens;
        private List<string> favoriteOpenedScreens;

        public FavoriteOpenedScreenListener()
        {
            this.key = "EME.FavoriteOpenedScreenListener." + this.GetServiceProxy().CurrentUserViewModel.CurrentUserDisplayName;
            this.recentlyOpenedScreens = this.GetServiceProxy().UserSettingsService.GetSetting<List<string>>(this.key);
            
            if (this.recentlyOpenedScreens == null)
            {
                this.recentlyOpenedScreens = new List<string>();
            }
            this.favoriteOpenedScreens = new List<string>();
            this.updateFavorites();
            this.GetServiceProxy().UserSettingsService.Closing += new EventHandler<EventArgs>(UserSettingsService_Closing);
            this.GetServiceProxy().NotificationService.Subscribe(typeof(ScreenOpenedNotification), OnScreenOpened);
        }

        void UserSettingsService_Closing(object sender, EventArgs e)
        {
            this.GetServiceProxy().UserSettingsService.SetSetting(this.key, this.recentlyOpenedScreens);
        }

        public void OnScreenOpened(Notification n)
        {
            ScreenOpenedNotification notification = n as ScreenOpenedNotification;
            if (notification.Screen != null)
            {
                string displayName = notification.Screen.DisplayName;
                this.recentlyOpenedScreens.Insert(0, displayName);
                this.updateFavorites();
            }
        }

        private void updateFavorites()
        {
            IEnumerable<string> newFavorites = this.recentlyOpenedScreens.Distinct();
            newFavorites.OrderBy(((s) => (s)), new StringComparer { recentlyOpenedScreens = this.recentlyOpenedScreens });
            this.favoriteOpenedScreens = new List<string>(newFavorites);
            this.OnPropertyChanged(() => this.FavoriteOpenedScreens);
        }

        public List<string> FavoriteOpenedScreens
        {
            get
            {
                return this.favoriteOpenedScreens;
            }
        }
    }
}